import time
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By

word = input('请输入你需要查询的产品')
f = open(f'{word}.csv', mode='a', encoding='GBK', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '标题',
    '价格',
    '评论',
    '详情页',
])
csv_writer.writeheader()  # 写入表头
driver = webdriver.Chrome()

driver.get("https://www.jd.com/")
driver.find_element(By.CSS_SELECTOR, '#key').send_keys(word)  # 定位输入框，输入商品名
driver.find_element(By.CSS_SELECTOR, '#search > div > div.form > button').click()  # 点击搜索按钮


# 下滑，用selenium操作js
def drop_down():
    for x in range(1, 12, 2):
        time.sleep(1)
        j = x / 9
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)


def get_shop_info():
    try:
        driver.implicitly_wait(10)
        drop_down()
        lis = driver.find_elements(By.CSS_SELECTOR, '#J_goodsList ul li')  # 获取所有的ul标签,返回一个列表
        for li in lis:
            title = li.find_element(By.CSS_SELECTOR, '.p-name em').text.replace('\n', '')
            price = li.find_element(By.CSS_SELECTOR, '.p-price i').text
            comment = li.find_element(By.CSS_SELECTOR, '.p-commit a').text
            href = li.find_element(By.CSS_SELECTOR, '.p-img a').get_attribute('href')
            dit = {
            '标题': title,
            '价格': price,
            '评论': comment,
            '详情页': href,
            }
            csv_writer.writerow(dit)
            print(title, price, comment, href)
    except:
        pass

for page in range(1, 5):
    print(f'=============正在采集第{page}页的数据内容=================')
    get_shop_info()
    driver.find_element(By.CSS_SELECTOR, '.pn-next').click()
driver.quit()  # 代码运行完关闭浏览器
